import { Controller, Get, Post, Body, UseGuards } from '@nestjs/common';
import { loginService } from './login.service';
import { User } from './login.entity';
import { AuthService } from '../../jwt/auth.service';
import { JwtAuthGuard } from '../../auth/jwt-auth.guard';

@Controller('login')
export class loginController {
  constructor(
    private readonly loginService: loginService,
    private readonly authService: AuthService,
  ) {}

  @Post()
  async findAll(@Body() loginData: any): Promise<any> {
    let access_token = await this.authService.generateToken(loginData);
    let data = await this.loginService.findLogin(loginData);
    if (data.length > 0) {
      return {
        code: 200,
        message: 'success',
        data: {
          access_token: access_token,
          user: data,
        },
      };
    } else {
      return {
        code: 400,
        message: 'error',
      };
    }
  }
  @UseGuards(JwtAuthGuard)
  @Get('/getInfo')
  async getUserInfo(): Promise<any> {
    return {
      code: 200,
    };
  }
  @Post('/register')
  async postRegister(@Body() loginData: any): Promise<any> {
    await this.loginService.userRegister(loginData);

    return {
      message: '注册成功',
      code: 200,
    };
  }
  // 使用redis
  @UseGuards(JwtAuthGuard)
  @Get('/redisFn')
  async redisFn(): Promise<any> {
    return {
      code: 200,
    };
  }
}
